//
//  TitleXQViewController.m
//  geiTA
//
//  Created by duyan on 14-11-20.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//




#import "TitleXQViewController.h"
#import "UIScrollView+TwitterCover.h"
#import "UIImageView+WebCache.h"
#import "HomeXQkindmodel.h"
#import <ShareSDK/ShareSDK.h>
#import "SpeakViewController.h"


@interface TitleXQViewController ()<UIWebViewDelegate, UIScrollViewDelegate>

@end

@implementation TitleXQViewController
- (void)dealloc
{
    [_image release];
    [_scrollview release];
    [_label release];
    [_model release];
    [_web release];
    [_str release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//  [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
   [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation_bar_background.png"] forBarMetrics:UIBarMetricsCompactPrompt];
    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
    
    self.navigationController.tabBarController.tabBar.hidden = YES;
    [self.image setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.image setImageWithURL:[NSURL URLWithString:_model.cover_image_url]];
    
    [self.label setFrame:CGRectMake(self.view.frame.size.width / 10, CHTwitterCoverViewHeight / 2.5f, self.view.frame.size.width / 10 * 8, 50)];
    [self.label setFont:[UIFont boldSystemFontOfSize:19.0]];
    [self.label setTextAlignment:NSTextAlignmentCenter];
    [self.label setNumberOfLines:0];
    [self.label setTextColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8]];
    [self.label setText:_model.title];
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, CHTwitterCoverViewHeight, self.view.frame.size.width, self.view.frame.size.height)];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_model.content_url]];
    [_web loadRequest:request];
    _web.scrollView.scrollEnabled = NO; 
    _web.delegate = self;
    self.scrollview = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [_scrollview setContentSize:_web.scrollView.contentSize];
    [_scrollview addTwitterCoverWithImage:_image.image];
    [_scrollview addSubview:_web];
    [_scrollview addSubview:_label];
    [self.view addSubview:_scrollview];
    _scrollview.delegate = self;
    [_scrollview release];
    [_web release];
    [_image release];
    [self creatLeftButton];
    [self creatTool];
    if (self.web.frame.origin.y == 0) {
        self.navigationItem.title = self.label.text;
        self.label.text = @"";
    }
}



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.image = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.label = [[UILabel alloc] initWithFrame:CGRectZero];
        self.model = [[HomeXQkindmodel alloc]init];
    }
    return self;
    
}


//webview
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
  

    // 加载完成 改变滚动范围
    [self.web setFrame:CGRectMake(0, CHTwitterCoverViewHeight, self.view.frame.size.width, webView.scrollView.contentSize.height + 113)];
    
    [self.scrollview setContentSize:webView.scrollView.contentSize];
    
  
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (self.web.frame.size.height > self.image.frame.size.height - CHTwitterCoverViewHeight ) {
        self.navigationItem.title = self.label.text;
//        self.label.text = @"";
//        if (self.navigationItem.title != nil) {
//        self.label.text = @"";
//    }

    }

        
    
}

-(void)changurl:(NSString *)str
{
    self.str = str;
}
- (void)creatLeftButton
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    [button setTitle:@"back" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:button];
    [self.navigationItem setLeftBarButtonItem:left];
    
}
#pragma mark-
#pragma mark-返回按钮
- (void)leftButtonAction:(id)sender
{
    if ([_web canGoBack]) {
        [_web goBack];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}





- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"%@", error);
}

-(void)creatTool
{


    UIImage *up = [UIImage imageNamed:@"up.png"];
    UIImage *down = [UIImage imageNamed:@"down.png"];
    UIImage *zou = [UIImage imageNamed:@"zou.png"];
    NSArray *array = [NSArray arrayWithObjects: up ,down , zou, nil];
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:array];
    [seg setFrame:CGRectMake(0, self.view.frame.size.height - 32, self.view.frame.size.width, 32)];
    [seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [seg setBackgroundColor:[UIColor whiteColor]];

   
    [self.view addSubview:seg];
    [seg release];
    
    
    
}
- (void)segAction:(UISegmentedControl*)seg
{
  
    switch (seg.selectedSegmentIndex) {
        case 0:
        {
            NSString *imagepath = [[NSBundle mainBundle]pathForResource:@"up" ofType:@"png"];
            id<ISSContent> publishContent = [ShareSDK content:@"写下你想说的话"
                                                defaultContent:@"默认分享内容测试，没内容时显示"
                                                         image:[ShareSDK imageWithPath:imagepath]
                                                         title:@"pmmq"
                                                           url:@"http://www.sharesdk.cn"
                                                   description:@"这是一条测试信息"
                                                     mediaType:SSPublishContentMediaTypeNews];
            [ShareSDK showShareActionSheet:nil shareList:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                if (state == SSResponseStateSuccess) {
                    NSLog(@"收藏成功");
                }
                else if(state == SSResponseStateFail)
                {
                    NSLog(@"分享失败");
                }
            }];
          
            seg.selectedSegmentIndex = -1;
          
            
        }
            break;
        case 1:
        {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(200, 400, 50, 50)];
            [label setBackgroundColor:[UIColor whiteColor]];
            [label setText:@"aa"];
            [label setTextColor:[UIColor redColor]];
            [self.view addSubview:label];
              seg.selectedSegmentIndex = -1;
            
        }
            break;
            
        case 2:
        {
            SpeakViewController *speak = [[SpeakViewController alloc]init];
            UINavigationController *na = [[UINavigationController alloc]initWithRootViewController:speak];
            [self presentViewController:na animated:YES completion:nil];
            
              seg.selectedSegmentIndex = -1;
            
        }
            break;
        default:
            break;
    }
}


-(void)changeXQmodel:(HomeXQkindmodel *)model
{
    self.model = model;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
